package cn.com.jenson.util;

import cn.com.jenson.common.BasicEntity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * List集合工具类
 */
public class ListUtil {

    /**
     * 判断集合是否为空集合或者为空
     * @param list 集合
     * @return 是/否
     */
    public static Boolean isEmpty(List<?> list){
        if (Objects.isNull(list)){
            return true;
        }
        return list.size() == 0;
    }

    public static String stringListToString(List<String> stringList){
        StringBuilder stringBuilder = new StringBuilder();
        stringList.forEach(item ->{
            stringBuilder.append(item).append(",");
        });
        return stringBuilder.toString();
    }

    public static List<String> stringToStringList(String str){
        return Arrays.stream(str.split(",")).collect(Collectors.toList());
    }

    public static String integerListToString(List<Integer> integerList){
        StringBuilder stringBuilder = new StringBuilder();
        integerList.forEach(item ->{
            stringBuilder.append(item).append(",");
        });
        return stringBuilder.toString();
    }

    public static List<Integer> integerToIntegerList(String str){
        return Arrays.stream(str.split(","))
                .map(Integer::parseInt)
                .collect(Collectors.toList());
    }

    public static <T> List<T> objectToObjectList(T t){
        List<T> tList = new ArrayList<>();
        tList.add(t);
        return tList;
    }

    public static <T extends BasicEntity> List<Integer> objectToObjectIdList(T t){
        List<T> tList = new ArrayList<>();
        tList.add(t);
        return tList.stream().map(T::getId).collect(Collectors.toList());
    }

    /**
     * 判断一个值是否存在集合里
     */
    public static <T> Boolean haveTheSameItem(List<T> tList,T t){
        return tList.stream().anyMatch(item -> item.equals(t));
    }

}
